<?php

class ServicesController extends AppController {
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session', 'Util', 'Upload');
  public $name = 'Services';
  public $paginate;


  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'order' => array(
            'Service.name' => 'asc'
            ));
  }

  function index() {
    $this->loadModel('Content');
    $this->set("title_for_layout","Services offerred at Allied Caterers in Trinidad and Tobago");
    $this->set("meta_keywords","Allied Caterers Services Catering Food Function Event");
    $this->set("meta_description","A description of the services offered by Allied Caterers of Trinidad and Tobago and its affiliates");

    $this->Content->id = 'ServicesMain';
    $servicesMain = $this->Content->read();
    $this->set('servicesMain', $servicesMain);
    $this->set('services', $this->paginate('Service'));
  }

  function industrial_catering() {
    $this->set("title_for_layout","Industrial Catering Services offerred at Allied Caterers in Trinidad and Tobago");
    $this->set("meta_keywords","Allied Caterers Industrial Catering Services");
    $this->set("meta_description","A description of the Industrial Catering Services offered by Allied Caterers of Trinidad and Tobago");
    //$this->set('services', $this->paginate('Service'));
    
  }  
  

    function details($serviceUrl) {
      if (empty($serviceUrl)) {
        throw new NotFoundException();
      } else {
      $service = $this->Service->find("first", array("conditions"=>array("Service.url"=>$serviceUrl)));
      if (!empty($service)) {
        $this->set("title_for_layout","Services Details at Allied Caterers");
        $this->set("meta_keywords","Allied Caterers Service Details");
        $this->set("meta_description","More detailed information on services that are offered by Allied Caterers");
        $this->set("service",$service['Service']);
      } else {
        throw new NotFoundException();
      }
      }
  }

  function menu () {
    $services = $this->Session->read('Services');
    if (empty($services)) {
      $services = $this->Service->find("all", array("order" => array("Service.name" => 'asc')));
      $this->Session->write('Services', $services);
    }
    return $services;
  }

    //admin

  function admin_index() {
    $this->layout="admin";
    $this->set('services', $this->paginate('Service'));
  }

  function admin_add() {
    $this->layout = "admin";
    $test = $this->Service->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }
    //admin
  function admin_edit($id = null) {
    $this->layout="admin";
    $this->Service->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Service->read();
    } else {
      //check for delete announcement
      if (isset($this->request->data['deleteBtn'])) {
        $this->admin_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

    private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Service->set($data);
      if ($this->Service->validates()) {

        $imageDetails = array(
            array ("id"=>"image_path", "name"=>"image_path", "width"=>Configure::read("App.servicesImageWidth"), "height"=>Configure::read("App.servicesImageHeight")),
            array ("id"=>"imagedetail1_path", "name"=>"image_detail_1_path", "width"=>Configure::read("App.servicesImageDetailWidth1"), "height"=>Configure::read("App.servicesImageDetailHeight1")),
            array ("id"=>"imagedetail2_path", "name"=>"image_detail_2_path","width"=>Configure::read("App.servicesImageDetailWidth2"), "height"=>Configure::read("App.servicesImageDetailHeight2")),
            array ("id"=>"imagedetail3_path", "name"=>"image_detail_3_path", "width"=>Configure::read("App.servicesImageDetailWidth3"), "height"=>Configure::read("App.servicesImageDetailHeight3"))
        );
        foreach($imageDetails as $img) {
          if (!empty($data["Service"][$img['name']]) && !empty($data["Service"][$img['name']]['name'])) {
            $imgName = $this->Util->cleanFileName($data["Service"][$img['name']]['name'], true) . "_" . date("U");
            $imageUploaded = $this->Upload->uploadImage($data["Service"][$img['name']], WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' .Configure::read('App.servicesImageFolder'),$img['width'],$img['height'], $imgName );
            $data["Service"][$img['id']] = Configure::read('App.servicesImageFolder') . '/' . $imageUploaded;
          } else {
            if($action==='add') {
              $data["Service"][$img['id']] = null;
            } else {
              $data["Service"][$img['id']] = isset($data["Service"]["_".$img['id']]) ? $data["Service"]["_".$img['id']] : null;
            }
          }
        }
        if ($this->Service->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          //getting previous image
          $data['Service']['image_path']=$data["Service"]["_image_path"];
          $data['Service']['imagedetail1_path']=$data["Service"]["_imagedetail1_path"];
          $data['Service']['imagedetail2_path']=$data["Service"]["_imagedetail2_path"];
          $data['Service']['imagedetail3_path']=$data["Service"]["_imagedetail3_path"];
          $data['Service']['id']=$id;
        }
        $this->request->data['Service'] = $data['Service'];
        $this->set('errors', $this->Service->validationErrors);
      }
      if($action==='add') {
        $this->render('admin_add');
      } else {
        $this->render('admin_edit');
      }
  }

  //admin
  function admin_delete($id) {
    $this->layout="admin";
    $this->admin_deleteMainImage($id, false);
    $this->admin_deleteDetailImage1($id, false);
    $this->admin_deleteDetailImage2($id, false);
    $this->admin_deleteDetailImage3($id, false);
    if ($this->Service->delete($id)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

  function admin_deleteMainImage ($id, $redirect=true) {
    $this->deleteImageType($id, 'image_path', $redirect);
  }

  function admin_deleteDetailImage1($id, $redirect=true) {
    $this->deleteImageType($id, 'imagedetail1_path', $redirect);
  }

   function admin_deleteDetailImage2($id, $redirect=true) {
    $this->deleteImageType($id, 'imagedetail2_path', $redirect);
  }

    function admin_deleteDetailImage3($id, $redirect=true) {
    $this->deleteImageType($id, 'imagedetail3_path', $redirect);
  }


  //$type [image_path, imagedetail1_path, imagedetail2_path, imagedetail3_path]
  private function deleteImageType($id, $type='image_path', $redirect=true) {
    $this->layout="admin";
    $image = $this->Service->read($type, $id);
    $imagePath = $image['Service'][$type];
    if (!empty($imagePath)) {
      $result = $this->Util->deleteFile(WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $imagePath);
      $this->Service->save(array('Service' => array('id' => $id, $type => '')), false, array($type));
      if($redirect){
        $this->Session->setFlash('The image has been deleted.');
        $this->redirect(array('action' => 'edit', $id));
      }
    }
  }

}

?>
